package dao;

import domain.StudentDomain;

public class StudentDao {
   //创建学生对象数组\对象
   private static StudentDomain [] student=new StudentDomain[8];
    //添加学生到数组或者对象
    public boolean addStudent(StudentDomain stu) {
        //定义变量index为-1，假设数组已经全部存满，没有null的元素
        int index=-1;
        //遍历数组取出每一个元素，判断是否是null
        for (int i = 0; i < student.length; i++) {
            StudentDomain domain=student[i];
            if (domain==null){
                //如果是null，让index变量记录当前索引位置，并使用break结束循环遍历
                index=i;
               break;
            }
        }
        //返回是否添加成功的boolean类型状态
        if(index==-1){
            //数组满了
            return false;
        }else {
            //数组未装满，正常添加,返回true
            student[index]=stu;
            return true;
        }
    }

    public StudentDomain[] findAllStudent() {
            return student;
    }

    public boolean deletStudent(String id) {
        int index=index(id);

        if (index==-1)return false;
        else {
            for (;index<student.length-1;){
                student[index]=student[++index];
            }
            return true;
        }
    }

    public int index(String id){
        int in=-1;
        for (int i = 0; i < student.length; i++) {
            StudentDomain stu=student[i];
            if(stu.getId().equals(id)){
                in=i;
                break;
            }
        }
        return in;
    }


}
